/*
    This file is part of JUltima.

    JUltima is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    JUltima is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with JUltima.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RunFrame.java
 *
 * Created on 2009-08-06, 12:44:09
 */

package jultima.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;

/**
 *
 * @author morti
 */
public class RunFrame extends javax.swing.JFrame implements MouseListener, MouseMotionListener {

    RunFrame thisFrame = this;
    MainFrame mainFrame=null;
    int scriptCount = 0;

    HashMap<String, RunScriptPane> paneMap = new HashMap<String, RunScriptPane>();

    ScriptEventListener scrEvtLis = new ScriptEventListener() {

	@Override
	public void scriptOpened(String name) {
	    RunScriptPane pane = new RunScriptPane(mainFrame, name);
	    GridBagConstraints c = new GridBagConstraints();
	    c.gridx = 0; c.gridy = scriptCount++;
	    thisFrame.add(pane, c);
	    thisFrame.pack();
	    paneMap.put(name, pane);
	}

	@Override
	public void scriptClosed(String name) {
	    System.out.println("Hello World");
	    RunScriptPane pane = paneMap.get(name);	    
	    remove(pane);
	    paneMap.remove(name);
	    thisFrame.pack();
	}

	@Override
	public void scriptRun(String name) {
	    RunScriptPane pane = paneMap.get(name);
	    pane.scriptRun();
	}

	@Override
	public void scriptPaused(String name) {
	    RunScriptPane pane = paneMap.get(name);
	    pane.scriptPaused();
	}

	@Override
	public void scriptStopped(String name) {
	    RunScriptPane pane = paneMap.get(name);
	    pane.scriptStopped();
	}

	
    };

    /** Creates new form RunFrame */
    public RunFrame(MainFrame mainFrame) {
	this.mainFrame = mainFrame;
	mainFrame.addScriptEventListener(scrEvtLis);
	setAlwaysOnTop(true);
        setFocusable(false);
        setLocationByPlatform(true);
	setLayout(new GridBagLayout());
	setUndecorated(true);
	setTitle("JUltima Toolbar");

	addMouseListener(this);
	addMouseMotionListener(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setTitle("JUltima Toolbar");
        setAlwaysOnTop(true);
        setFocusable(false);
        setLocationByPlatform(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 192, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 123, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    Point lastLocation = new Point();
    public void mouseDragged(MouseEvent e) {
	System.out.println(""+e.getX());
	int offX = e.getX() - lastLocation.x;
	int offY = e.getY() - lastLocation.y;
	System.out.println("" + offX + ", " + offY);
	Point p = getLocation();
	setLocation(new Point(
	    p.x + offX,
	    p.y + offY)
	);
    }

    public void mouseMoved(MouseEvent e) {
	lastLocation.x = e.getX();
	lastLocation.y = e.getY();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
